<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;
use app\admin\model\Coupon;
use app\admin\model\Integralrecord;

use think\Request;

class Integralproduct extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->defaultOrder = 'create_date DESC';

        if($this->request->action() == 'add' || $this->request->action() == 'edit'){
            $coupons = Coupon::where(['coupon_purpose' => 2, 'status' => 1, 'is_del' => 0])->select();

            $this->assign('coupons', $coupons);
        }
    }

    public function index(){

        return parent::index();
    }

    /**
     * Add
     */
    public function add(Request $request){

        return parent::add($request);
    }

    /**
     * Edit
     */
    public function edit(Request $request){

        return parent::edit($request);
    }

    /**
     * Save Post
     */
    public function savePost(Request $request){
        $model = model($this->model);

        if ($request->isPost()) {
            $data = $request->param();

            if ($data['id']) {
                // Update data
                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
                $logInfo = $this->admin->name . '更新了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_update';
            } else {
                // Insert data
                $data['id'] = Helper::getUUID();
                $data['status'] = 1;
                $result = $model->save($data);
                $logInfo = $this->admin->name . '添加了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_insert';
            }

            if($result !== false) {
                // Query执行后的操作
                $model->$afterAction($data);

                // 写入日志
                Common::adminLog($request, $logInfo);

                return $this->success('保存成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }

    /**
     * 启用/禁用
     */
    public function handle(Request $request){
        $model = model($this->model);

        if ($request->isPost()) {
            $data = $request->param();

            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);

            if($result !== false) {
                return $this->success('操作成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }

    /**
     * 处理列表数据
     */
    public function handleListData($data){
        foreach ($data as $item) {
            $couponInfo = Coupon::where('id', $item['coupon_id'])->find();
            if($couponInfo){
                $item['coupon_name'] = $couponInfo['name'];
                $item['coupon_number'] = $couponInfo['number'];

                // 是否过期
                if (strtotime($couponInfo['end_time']) < time()) {
                    $item['is_expired'] = '优惠券已过期';
                } else {
                    $item['is_expired'] = '优惠券未过期';
                }
            }
            // 兑换人数
            $item['member_num'] = Integralrecord::where('integral_id', $item['id'])->group('member_id')->count();
        }

        return $data;
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if(isset($param['coupon_name']) && $param['coupon_name']){
            $couponIds = Coupon::where(['name' => ['like', '%'.$param['coupon_name'].'%']])->column('id');
            $where['coupon_id'] = ['in', $couponIds];
        }

        return $where;
    }

    /**
     * 积分规则
     */
    public function integralRule(Request $request){
        $model = db('integral_rule');
        //$id = $request->param('id');
        // Get data info
        $info = $model->find();

        if(!$info){
            $info = [];
        }

        return $this->fetch('integralrule', [
            'info' => $info
        ]);
    }

    /**
     * 保存积分规则
     */
    public function saveIntegralRule(Request $request){
        $model = db('integral_rule');

        if ($request->isPost()) {
            $data = $request->param();

            if ($data['id']) {
                $data['update_date'] = date('Y-m-d H:i:s');
                $result = $model->where(['id' => $data['id']])->update($data);
            } else {
                $data['create_date'] = date('Y-m-d H:i:s');
                $result = $model->insert($data);
            }

            if($result !== false) {
                return $this->success('操作成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }

}